<?php
header('Content-type: text/plain; charset=utf-8');
header("Pragma: no-cache");
header("Expires: 0");


require("includes/mod_function.inc.php");


  $purl = $_REQUEST['url'];
  $puri = $_REQUEST['uri'];

 //access by http://localhost:8080/index.php?u=www.eetchina.com
 //echo "parameter u=".$_GET["u"]."<br><br>";
 $fs = new FoxySite();
 global $foxydb;
 if($_GET["getfav"]=='true'){
	$usersid = ip_address_to_number($_SERVER['REMOTE_ADDR']);
	$foxydb->open();
	$result = $foxydb->selectUserFavoriteSites($usersid,100);

	//get this system website as one of the user's favorite site
	$defSite = $foxydb->findSiteByUri("http://".$_SERVER["HTTP_HOST"]);
	$foxydb->close();

	//print_r($result);

	if($result){
		foreach($result as $site){
			$ret['site'][] = array($site['sid'],$site['uri'],$site['name'],$site["searchpath"],$site["rsspath"],$site['favoriteranking'],$site["ranking"]);
		}
	}
	//return defSite as user favorite site is used to display default 最新头条
	//print_r($defSite);
	if($defSite) $ret['site'][] = array($defSite['sid'],$defSite['uri'],$defSite['name'],$defSite["searchpath"],$defSite["rsspath"],1,$defSite["ranking"]);

	

 }else if($puri){ //get related sites information

	 $result = $fs->getRelatedSitesByUriPrePath($puri);

	 if($result["site"]){
		 foreach ($result["site"] as $k => $site){
			 $retSite = array($site["sid"],$site["uri"],$site["name"],$site["searchpath"],$site["rsspath"],$site["ranking"]);
			 $ret["site"][] = $retSite; 
		 }
	 }

	 if($result["s2c"]){
		 foreach ($result["s2c"] as $s2c){
			 $rets2c = array($s2c["sid"],$s2c["sitesid"],$s2c["catsid"]);
			 $ret["s2c"][] = $rets2c; 
		 }
	 }

	 //the primary path for the site identified by $_GET["u"]
	 if($result["primarypath"]){
		 foreach ($result["primarypath"] as $cat){
			 //$retcat = array($cat["sid"],$cat["psid"],$cat["name"]);
			 $ret["path"][] = $cat["sid"]; 
		 }
	 }
	 if ($result["primaryareapath"]){
		 foreach ($result["primaryareapath"] as $cat){
			 //$retcat = array($cat["sid"],$cat["psid"],$cat["name"]);
			 $ret["areaPath"][] = $cat["sid"]; 
		 }
	 }
 }else if ($purl){ //get related links information
	$usersid = ip_address_to_number($_SERVER['REMOTE_ADDR']);
	$foxydb->open();
	$foxydb->saveLinkLog($purl, $usersid);
	$foxydb->close();

	$ret = $fs->getHotLinksWithinDays(10,5);
 }

/*
if(!function_exists('json_encode'))
{
	include_once('JSON.php');
	$GLOBALS['JSON_OBJECT'] = new Services_JSON();
	function json_encode($value)
	{
	   return $GLOBALS['JSON_OBJECT']->encode($value);
	}

	function json_decode($value)
	{
	   return $GLOBALS['JSON_OBJECT']->decode($value);
	}
}
 $jsonObject = json_encode($ret);
*/
include_once('includes/JSON.php');
$GLOBALS['JSON_OBJECT'] = new Services_JSON();
$jsonObject = $GLOBALS['JSON_OBJECT']->encode($ret);

//$jsonObject = json_encode($ret);
echo $jsonObject;

?>